package learn;

import java.util.Arrays;

/**
 * 贪心算法
 */
public class GreedySulution {

    /**
     * 算法-- 有限小胃口
     * @param g 孩子胃口
     * @param s 饼干
     * @return
     */
    public int findContentChildren(int g[],int s[]){
        //先对数字进行排序
        Arrays.sort(g);
        Arrays.sort(s);

        int count = 0;
        int start = 0;

        for (int i = 0;i< g.length && start<s.length;i++){
            if (s[start]>=g[i]){
                start++;
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args){
        GreedySulution greedySulution = new GreedySulution();
        int[] g = {1,2,3,5};
        int[] s = {6,2,1};

        int a =greedySulution.findContentChildrenBig(g,s);

        System.out.println(a);
    }

    /**
     * 算法-- 先满足胃口大的
     * @param g 孩子胃口
     * @param s 饼干
     * @return
     */
    public int findContentChildrenBig(int g[],int s[]){
        //先对数字进行排序
        Arrays.sort(g);
        Arrays.sort(s);

        int count = 0;
        int start = s.length-1;

        for (int i = g.length-1;i>=0;i--) {
            if (start >= 0 && g[i] <= s[start]) {
                start--;
                count++;
            }
        }
        return count;
    }

}
